﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using H2.Streams;

namespace Mystery.SCNR.Scripts
{
    public class ScriptGlobal
    {
        public const int Offset = 448;
        public const int ChunkSize = 40;

        public string Name;

        public FunctionTypes Type;

        public VariableTypes ReturnType;

        public short SyntaxChunkNumber;
        public ushort InitializationExpressionIndex;

        public override string ToString()
        {
            return Name;
        }

        public static ScriptGlobal[] Read(MapStream map, int scnrOffset)
        {
            ScriptGlobal[] scriptGlobals = new ScriptGlobal[map.ReadReflexiveAt(scnrOffset + Offset)];

            for (int i = 0; i < scriptGlobals.Length; i++)
            {
                ScriptGlobal sf = new ScriptGlobal();
                scriptGlobals[i] = sf;

                sf.Name = map.ReadString(32, false);
                sf.Type = (FunctionTypes)map.ReadInt16();
                sf.ReturnType = (VariableTypes)map.ReadInt16();
                sf.SyntaxChunkNumber = map.ReadInt16();
                sf.InitializationExpressionIndex = map.ReadUInt16();
            }

            return scriptGlobals;
        }
    }
}
